﻿using Asp.Versioning;
using FlyingEye.DeviceMaintenance.Repositories;
using FlyingEye.DeviceMaintenanceServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    /// <summary>
    /// 保养项目控制器
    /// </summary>
    [ApiVersion("1.0")]
    [Route("api/device-ops-service/v{version:apiVersion}/device-maintenances")]
    [ControllerName("DeviceMaintenance")]
    [Authorize]
    public class DeviceMaintenanceController : DefaultController
    {
        private readonly DeviceMaintenanceService _deviceMaintenanceService;

        private readonly DevicePartService _devicePartService;

        private readonly MaintenanceMemberService _maintenanceMemberService;

        private readonly IDeviceMaintenanceRepository _deviceMaintenanceRepository;

        public DeviceMaintenanceController(
            ILogger<DeviceMaintenanceController> logger,
            DeviceMaintenanceService deviceMaintenanceService,
            DevicePartService devicePartService,
            MaintenanceMemberService maintenanceMemberService,
            IDeviceMaintenanceRepository deviceMaintenanceRepository)
        {
            _deviceMaintenanceService = deviceMaintenanceService;
            _devicePartService = devicePartService;
            _maintenanceMemberService = maintenanceMemberService;
            _deviceMaintenanceRepository = deviceMaintenanceRepository;
        }

        /// <summary>
        /// 获取项目成员
        /// </summary>
        [HttpGet("{id}/members")]
        public async Task<ActionResult<List<MaintenanceMemberResponse>>> GetMembersAsync(Guid id)
        {
            return await GetInvokeAsync(async () =>
            {
                return await _maintenanceMemberService.GetListByDeviceMaintenance(id);
            });
        }

        [HttpGet("{id}", Name = "GetByProjectId")]
        public async Task<ActionResult<DeviceMaintenanceResponse>> GetAsync(Guid id)
        {
            return await GetInvokeAsync(async () =>
            {
                return await _deviceMaintenanceService.GetAsync(id);
            });
        }

        [HttpGet("{maintenanceId}/parts")]
        public async Task<ActionResult<List<DevicePartResponse>>> GetPartsByDeviceMaintenanceAsync(Guid maintenanceId)
        {
            return await GetInvokeAsync(async () =>
            {
                return await _devicePartService.GetByDeviceMaintenanceAsync(maintenanceId);
            });
        }

        [HttpGet()]
        public async Task<ActionResult<List<DeviceMaintenanceResponse>>> GetListAsync()
        {
            return await GetInvokeAsync(async () =>
            {
                return await _deviceMaintenanceService.GetListAsync();
            });
        }

        [HttpPost]
        public async Task<ActionResult<DeviceMaintenanceResponse>> InsertAsync([FromBody] CreateDeviceMaintenanceRequest deviceMaintenance)
        {
            return await InsertInvokeAsync(async () =>
            {
                return await _deviceMaintenanceService.InsertAsync(deviceMaintenance);
            }, "GetByProjectId", (result) => new { id = result.Id });
        }

        [HttpPut]
        public async Task<ActionResult<DeviceMaintenanceResponse>> UpdateAsync([FromBody] UpdateDeviceMaintenanceRequest deviceMaintenance)
        {
            return await PutInvokeAsync(async () =>
            {
                return await _deviceMaintenanceService.UpdateAsync(deviceMaintenance);
            });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await DeleteInvokeAsync(async () =>
            {
                await _deviceMaintenanceService.DeleteAsync(id);
            });
        }
    }
}
